import path from 'path'
import fs from 'fs'

console.log('fs:', '\n', Object.getOwnPropertyNames(fs))

const assetPath = path.join(path.dirname(process.cwd()), 'asset')
const fsFile = path.join(assetPath, 'fs.txt')
fs.promises.readFile(fsFile, 'utf-8').then((res) => {
	console.log('promise style:', '\n', res)
})

/**
 * 回调形式，异步读取
 */
fs.readFile(fsFile, 'utf-8', (err, data) => {
	if (!err) {
		console.log('\nnode style: \n', data)
	} else {
		console.error(err)
	}
})

/**
 * 获取文件或者目录的基本信息
 */
const fileInfo = fs.statSync(fsFile)
console.log('fs file info: \n', fileInfo, fileInfo.isFile(), fileInfo.isDirectory())
console.log('asset info: \n', fs.statSync(assetPath).isFile(), fs.statSync(assetPath).isDirectory())

/**
 * 写入文件
 */
fs.writeFileSync(path.join(assetPath, 'test.txt'), 'this is test word!')

/**
 * appendFileSync
 * 向文件末尾追加写入内容
 */
fs.appendFileSync(path.join(assetPath, 'test.txt'), ' this is append word!')
fs.readFile(path.join(assetPath, 'test.txt'), 'utf-8', (err, res) => {
	if (!err) {
		console.log('test file word: \n', res)
	} else {
		console.log(err)
	}
})

/**
 * 获取指定目录下所有文件的绝对路径。
 */
function getAllFiles(dirPath, arrayOfFiles) {
	const files = fs.readdirSync(dirPath, { withFileTypes: true })

	arrayOfFiles = arrayOfFiles || []

	files.forEach((f) => {
		if (f.isDirectory()) {
			arrayOfFiles = getAllFiles(path.resolve(dirPath, f.name), arrayOfFiles)
		} else {
			arrayOfFiles.push(path.normalize(path.resolve(dirPath, f.name)))
		}
	})
	return arrayOfFiles
}
console.log(getAllFiles('./'))

/**
 * 组合
 */
// eslint-disable-next-line no-unused-vars
const combination = [
	{
		type: 'gold',
		name: '黄金'
	},
	{
		type: 'realty',
		name: '不动产'
	},
	{
		type: 'antique',
		name: '古董'
	},
	{
		type: 'stock',
		name: '股票'
	},
	{
		type: 'fund',
		name: '基金',
		info: '晨星咨询',
		buy: [
			// 买入
			'期限-长、短、定',
			'个人风格-核心项',
			'基金公司-经理',
			'仓位分配-核辅比例',
			'一次性&定投-奖金&月结余'
		],
		manage: [
			// 管理
			'再调整-强制【高抛低吸】',
			'长期持有-同类切换【经理、公司、行业股票占比变动等】',
			'【警惕】跑的太快、慢。规模调整太大、小-同类相比'
		],
		sales: [
			// 出售
			'不再符合个人投资策略-可转再调整',
			'投资目标已达到-卖'
		],
		child: [
			{
				type: 'main',
				name: '核心',
				code: ''
			},
			{
				type: 'pursue',
				name: '追求',
				code: ''
			}
		]
	}
]

// eslint-disable-next-line no-unused-vars
const _2023 = [
	{
		type: 'house loan',
		thing1: 'tranfer public fund',
		thing2: 'repay the loan ahead of time'
	},
	{}
]
